#!/bin/sh

# +++ 变量声明 Start+++
export g_libId="ffmpeg-cli-framework"
# 源码-库目录
export g_sourceLibDir=${g_inputRootDir}/${g_libId}
# 脚本-库目录
g_scriptLibDir=${g_scriptArchDir}/${g_libId}
# Build-库目录
export g_buildLibDir=${g_buildArchDir}/${g_libId}
if [[ ! -e ${g_buildLibDir} ]]; then
    mkdir -p ${g_buildLibDir}
fi

# Output-库目录
export g_outputLibDir=${g_outputArchDir}/${g_libId}
if [[ ! -e ${g_outputLibDir} ]]; then
    mkdir -p ${g_outputLibDir}
fi

# 执行buildLibDir的makefile
function customMake() {
    # make ffmpeg/$g_arch
    # make wrapper/${g_arch}
    # make wrapper_ios/${g_arch}
    make wrapper_framework/$g_arch
}

customConfigure() {
    # 清除历史文件.
    [[ -e ${g_buildLibDir}/Makefile ]] && rm $g_buildLibDir/Makefile
    [[ ! -e $g_buildLibDir/lib ]] && mkdir -p $g_buildLibDir/lib

    CMAKE_CONFIG=$([[ $g_compileMode == $COMPILE_MODE_DEBUG ]] && echo Debug || echo Release)
    
    cat <<END >>${g_buildLibDir}/Makefile


default_target: all
.PHONY : default_target

clean:
	rm -rf Makefile objs 

ffmpeg/${g_arch}:
	@cd ${g_buildArchDir}/ffmpeg-4.2-ugc && \\
    echo "make \$@" && \\
	\$(MAKE) -j4 && \\
	\$(MAKE) -j4 install-headers && \\
    find ./ -name "*.o"|xargs $_AR rcs libffmpeg.a && \\
	cp -f libffmpeg.a ${g_buildLibDir}/lib/

wrapper/${g_arch}:
	@cd ${g_buildArchDir}/ffmpeg-cli-wrapper && \\
	echo "make \$@" && \\
	

wrapper_ios/${g_arch}:
	@cd ${g_buildArchDir}/ffmpeg-cli-wrapper-ios && \\
	echo "make \$@" && \\
	cmake --build . -j 4 --target install --config ${CMAKE_CONFIG}

wrapper_framework/${g_arch}: ffmpeg/${g_arch} wrapper/${g_arch} wrapper_ios/${g_arch}
	@mkdir -p ${g_buildArchDir}/../framework && \\
	rm -rf ${g_buildArchDir}/../framework/ffmpeg_cli_wrapper.framework && \\
	cp -rf ${g_buildArchDir}/lib/ffmpeg_cli_wrapper.framework ${g_buildArchDir}/../framework/

all: wrapper_framework/${g_arch} 
	@echo "ALL DONE. "
	@echo "framework location: objs/ffmpeg_cli_wrapper.framework"
.PHONY : all

END
}
